package com.knowledgePointsByMyTest.thread;

import java.util.concurrent.Exchanger;

/**
 * @author yulongtian
 * @create 2024-12-11 11:49
 */
class Main {
    public static void main(String[] args) {
        Exchanger<String> exchanger = new Exchanger<>();
        Object lock = new Object();
        Thread thread1 = new Thread(() -> {
            try {
                String message = "Message from thread1";
                String response = exchanger.exchange(message);
                System.out.println("Thread1 received: " + response);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        });

        Thread thread2 = new Thread(() -> {
            try {
                String message = "Message from thread2";
                String response = exchanger.exchange(message);
                System.out.println("Thread2 received: " + response);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        });

        thread1.start();
        thread2.start();
    }
}